layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var laydate = layui.laydate;

    laydate.render({
    	elem:'#startTime'
    });
    
    laydate.render({
    	elem:'#endTime'
    });

    laydate.render({
    	elem:'#startTime2'
    });
    
    laydate.render({
    	elem:'#endTime2'
    });

    laydate.render({
      elem: '#startTime3'
        	,value:new Date()
    });

    laydate.render({
      elem: '#endTime3'
        	,value:new Date()
    });
    
    /**
     * Outbound Order
     */
    var OutOrder = {
        tableId: "outOrderTable"
    };
    
    /**
     * Initialize Outbound Order
     */
    OutOrder.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'Primary Key'},
            {field: 'customerNumber', title: 'Customer Order Number', style:'color: #2b85e4;font-weight: 600;',width:150,event: 'detailedSign'},
            {field: 'outOrderStatus', sort: true, title: 'Status',templet: '#outOrderStatusTpl'},
            {field: 'setMentiontime', sort: true, title: 'Shipment Date'},
            {field: 'createUserName', title: 'Order Creator'},
            {field: 'getUnit', sort: true, title: 'Receiving Unit', style:'font-weight: 600;',width:180},
            {field: 'getLocation', sort: true, title: 'Unloading Location',width:250},
            {field: 'outWay', title: 'Outbound Method'},
            {field: 'note', title: 'Remarks',width:120, totalRowText: 'Total:'},
            {field: 'sumVolume', title: 'Volume', totalRow: true},
            {field: 'sumNum', title: 'Quantity', totalRow: true},
            {field: 'sumMoney', title: 'Value', totalRow: true},
            {field: 'getUnitId', title: 'Receiving Unit ID'},
            {field: 'kilometer', title: 'Kilometer'},
            {field: 'storehouseName', title: 'Warehouse'},
            {field: 'outCustomerName', title: 'Customer'},
            {field: 'outOrderNumber', sort: true, title: 'Outbound Order No.',event: 'detailedSign'},
            {field: 'setUnit', sort: true, title: 'Loading Unit'},
            {field: 'setLocation', sort: true, title: 'Loading Address'},
            {field: 'createTime', sort: true, title: 'Creation Time'},
            {field: 'submitTime', sort: true, title: 'Submission Time'},
            {field: 'submitterName', title: 'Submitter'},
            {field: 'examineTime', sort: true, title: 'Confirmation Time'},
            {field: 'examineName', title: 'Confirmer'},
            {field: 'finishOutTime', sort: true, title: 'Outbound Completion Time'},
            {fixed: 'right',width: 90,align: 'center', toolbar: '#tableBar', title: 'Operations'}
        ]];
    };
        
        /**
         * 点击查询按钮
         */
        OutOrder.search = function () {
            var queryData = {};
            queryData['condition'] = $("#condition").val();
        	queryData['outCustomerId'] = $("#outCustomerId").val();
            queryData['outStorehouseId'] = $("#outStorehouseId").val();
            queryData['outOrderStatus'] = $("#outOrderStatus").val();
	        queryData['startTime'] = $("#startTime").val();
	        queryData['endTime'] = $("#endTime").val();
	        queryData['startTime2'] = $("#startTime2").val();
	        queryData['endTime2'] = $("#endTime2").val();
	        queryData['startTime3'] = $("#startTime3").val();
	        queryData['endTime3'] = $("#endTime3").val();
            table.reload(OutOrder.tableId, {
                where: queryData, page: {curr: 1},
                 done:function () {
                }
            });
        };

    /**
     * 导出excel按钮
     */
    OutOrder.exportExcel = function () {
        var checkRows = table.checkStatus(OutOrder.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };
        
        // 渲染出库订单表格
        var tableResult = table.render({
            elem: '#' + OutOrder.tableId,
            url: Feng.ctxPath + '/outOrder/getQuerylist',
            page: true,
            limit: 60,
			limits:[40,60,120,240,360,480,1200],
        	height: "full-60",
            cellMinWidth: 100,
            totalRow: true ,//Enable total row
        	toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            cols: OutOrder.initColumn()
        });

        // 搜索按钮点击事件
        $('#btnSearchout').click(function () {
        	OutOrder.search();
        });

	    // 导出excel
	    $('#btnExp').click(function () {
	        OutOrder.exportExcel();
	    });
	
	    // 导出excel
	    $('#btnExpBySpec').click(function () {
	        OutOrder.exportExcelBySpec();
	    });
	
	    /**
	     * 导出excel按钮
	     */
	    OutOrder.exportExcelBySpec = function () {
			window.location.href = Feng.ctxPath + '/outOrder/exportExcelBySpec?outOrderStatus='+$("#outOrderStatus").val()+"&condition2="+$("#condition").val()
			+"&outCustomerId="+$("#outCustomerId").val()+"&outStorehouseId="+$("#outStorehouseId").val()
			+"&getUnit="+$("#getUnit").val()
			+"&startTime="+$("#startTime").val()+"&endTime="+$("#endTime").val()
			+"&startTime2="+$("#startTime2").val()+"&endTime2="+$("#endTime2").val();
	    };

	    // 单击行事件
	    table.on('row(' + OutOrder.tableId + ')', function (obj) {
	        var data = obj.data;
			$('div[lay-id="' + OutOrder.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
			$('div[lay-id="' + OutOrder.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
	    });
        
        // 工具条点击事件
        table.on('tool(' + OutOrder.tableId + ')', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'revoke') {
            	OutOrder.revoke(data);
            } else if (layEvent === 'Launch') {
            	OutOrder.launch(data);
            } else if (layEvent === 'detailedSign') {
                layer.open({
                    type: 2,
                    title: 'Order Details',
                    area: ['1000px', '500px'],
                    anim: 5,
                    content: Feng.ctxPath + '/outOrderDetailed/toQuery?detailedOutNumber=' + data.outOrderNumber,
                });
            }
        });
        
        /**
         * 撤回
         */
        OutOrder.revoke = function (data) {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/outOrder/revoke?id="+data.id, function (data) {
                    if(data.success){
                    	Feng.success("Withdrawal successful!");
                		table.reload(OutOrder.tableId);
                    } else {
                        Feng.error("Withdrawal failed!" + data.message + "!");
                		table.reload(OutOrder.tableId);
                    }
                }, function (data) {
                    Feng.error("Withdrawal failed!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("Confirm withdrawal of selected order?", operation);
        };

        /**
         * 发起
         */
        OutOrder.launch = function (data) {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/outOrder/launch?id="+data.id, function (data) {
                    if(data.success){
                    	Feng.success("Launch successful!");
                		table.reload(OutOrder.tableId);
                    } else {
                        Feng.error("Launch failed!" + data.message + "!");
                    }
                }, function (data) {
                    Feng.error("Launch failed!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("Re-launch selected order?", operation);
        };

});
